package com.jd.ipservice;

import java.io.Serializable;

public class Cidr implements Serializable {
	private static final long serialVersionUID = -4962979657552809590L;
	private final IpSet ipSet;
	private final Long start;
	private final Long end;
	private final String cidr;

	public Cidr(IpSet ipSet, long start, long end, String cidr) {
		super();
		this.ipSet = ipSet;
		this.start = start;
		this.end = end;
		this.cidr = cidr;
	}

	public IpSet getIpSet() {
		return ipSet;
	}

	public Long getStart() {
		return start;
	}

	public Long getEnd() {
		return end;
	}

	public String getCidr() {
		return cidr;
	}

	public boolean contains(long qip) {
		return start <= qip && qip <= end;
	}

	public Cidr compete(Cidr cidr) {
		String[] selfNames = this.ipSet.getName().split("-");
		String[] otherNames = cidr.ipSet.getName().split("-");
		if (!selfNames[2].equals(otherNames[2])) {
			if (selfNames[2].equals("OTHER\"")) {
				return cidr;
			}
			return this;
		} else {
			if (selfNames[1].equals("OTHER\"")) {
				return cidr;
			}
			return this;
		}
	}

}
